package com.itheima.reggie.controller;

import cn.hutool.core.bean.BeanUtil;
import cn.hutool.core.collection.CollUtil;
import cn.hutool.core.util.StrUtil;
import com.baomidou.mybatisplus.core.conditions.Wrapper;
import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.itheima.reggie.common.R;
import com.itheima.reggie.dto.SetmealDto;
import com.itheima.reggie.entity.Category;
import com.itheima.reggie.entity.Setmeal;
import com.itheima.reggie.service.CategoryService;
import com.itheima.reggie.service.SetmealService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.web.bind.annotation.*;

import java.util.ArrayList;
import java.util.HashMap;
import java.util.List;
import java.util.Map;
import java.util.stream.Collectors;

@RestController
@RequestMapping("/setmeal")
public class SetmealController {

    @Autowired
    private SetmealService setmealService;

    @Autowired
    private CategoryService categoryService;

    /**
     * 新增套餐
     * @param setmealDto
     * @return
     */
    @PostMapping
    public R saveSetmeal(@RequestBody SetmealDto setmealDto){

        return setmealService.saveSetmeal(setmealDto);
    }


    @GetMapping("/page")
    public R queryPage(Integer page,Integer pageSize,String name){

        IPage<Setmeal> pageInfo = new Page<>(page,pageSize);
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.like(StrUtil.isNotEmpty(name),Setmeal::getName,name);
        setmealService.page(pageInfo,qw);

        long total = pageInfo.getTotal();//总数
        List<Setmeal> records = pageInfo.getRecords();
        // List<Setmeal>   -->  List<SetmealDto>
        List<SetmealDto> setmealDtoList = new ArrayList<>();
        for (Setmeal setmeal : records) {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal,setmealDto);
            Long categoryId = setmealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());//分类名称
            // setmealDto.setSetmealDishes(null);  //套餐对应的菜品的集合，好像用不到
            setmealDtoList.add(setmealDto);
        }

        List<SetmealDto> collect = records.stream().map(setmeal -> {
            SetmealDto setmealDto = new SetmealDto();
            BeanUtil.copyProperties(setmeal, setmealDto);
            Long categoryId = setmealDto.getCategoryId();
            Category category = categoryService.getById(categoryId);
            setmealDto.setCategoryName(category.getName());
            return setmealDto;
        }).collect(Collectors.toList());


        Map map = new HashMap();
        map.put("total",total);
        map.put("records",collect);

        return R.success(map);
    }


    @DeleteMapping
    public R deleteByIds(@RequestParam("ids") List<Long> ids){
        return categoryService.deleteByIds(ids);
    }


    @GetMapping("/list")
    public R getSetmealByCategoryId(Long categoryId){
        // select * from setmeal where category_id = ?
        LambdaQueryWrapper<Setmeal> qw = new LambdaQueryWrapper<>();
        qw.eq(Setmeal::getCategoryId,categoryId);
        List<Setmeal> list = setmealService.list(qw);
        return R.success(list);
    }
}
